package com.player.thirdGroup.service;

import com.player.thirdGroup.dao.DeptDao;
import com.player.thirdGroup.dao.LogDao;
import com.player.thirdGroup.dao.MessageDao;
import com.player.thirdGroup.dao.PlayerDao;
import com.player.thirdGroup.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author VULCAN
 * @create 2019-12-28 7:48
 * @desc 运动员
 **/
@Service
public class PlayerService {
    @Autowired
    private PlayerDao playerDao;
    @Autowired
    private LogDao logDao;
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private MessageDao messageDao;
    //全部信息
    public Page<Player> findAll(Player player, int page, int size) {
        Specification<Player> specification = new Specification<Player>() {
            //分页
            //new  一个条件模糊对象
            ArrayList<Predicate> predicates = new ArrayList<Predicate>();

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                //判断用户名不为空
                if (player.getName() != null) {
                    Predicate player_name = criteriaBuilder.like(root.get("name"), "%" + player.getName() + "%");

                    predicates.add(player_name);
                }
                //判断
                if (player.getSex() != null) {
                    Predicate player_sex = criteriaBuilder.equal(root.get("sex"), player.getSex());
                    predicates.add(player_sex);
                }

                Predicate[] predicates = this.predicates.toArray(new Predicate[this.predicates.size()]);
                return criteriaBuilder.and(predicates);
            }
        };
        //分页
        PageRequest of = PageRequest.of(page - 1, size);
        Page<Player> all = playerDao.findAll(specification, of);
        return all;
    }
    //列表
    public List<Player> findAll(){
        return playerDao.findAll();
    }
    //通过
    public Player deptunderpass(String id) throws UnknownHostException {
        Player player = playerDao.findById(id).get();
        //通过
        player.setStatus("1");
        playerDao.save(player);
        //通过日志
        Log log = new Log();
        log.setId(UUID.randomUUID().toString());
        log.setDate(new Date());
        log.setName(player.getName());
        InetAddress ip4 = Inet4Address.getLocalHost();
        System.out.println(ip4.getHostAddress());
        log.setIp(ip4.getHostAddress());
        Dept dept = deptDao.findById(player.getDept().getId()).get();
        log.setContent("单位审核-"+dept.getName()+"-审核成功");
        logDao.save(log);
        return player;
    }
    //不通过
    public Player deptundernopass(String id) throws UnknownHostException {
        Player player = playerDao.findById(id).get();
        //不通过
        player.setStatus("2");
        playerDao.save(player);

        //不通过日志
        Log log = new Log();
        log.setId(UUID.randomUUID().toString());
        log.setDate(new Date());
        log.setName(player.getName());
        InetAddress ip4 = Inet4Address.getLocalHost();
        System.out.println(ip4.getHostAddress());
        log.setIp(ip4.getHostAddress());
        Dept dept = deptDao.findById(player.getDept().getId()).get();
        log.setContent("单位审核-"+dept.getName()+"-审核失败");
        logDao.save(log);
        return player;
    }
    /**
     * 批量通过
     */
    public String bulkpass(Long[] ids) throws UnknownHostException {
        for(Long id:ids){
            Player player = playerDao.findById(id+"").get();
            player.setStatus("1");
            playerDao.save(player);
            //通过日志
            Log log = new Log();
            log.setId(UUID.randomUUID().toString());
            log.setDate(new Date());
            log.setName(player.getName());
            InetAddress ip4 = Inet4Address.getLocalHost();
            System.out.println(ip4.getHostAddress());
            log.setIp(ip4.getHostAddress());
            Dept dept = deptDao.findById(player.getDept().getId()).get();
            log.setContent("单位审核-"+dept.getName()+"-审核成功");
            logDao.save(log);

        }
        return null;
    }
    /**
     * 批量不通过
     */
    public String bulknopass(Long[] ids) throws UnknownHostException {
        for(Long id:ids){
            Player player = playerDao.findById(id+"").get();
            player.setStatus("2");
            playerDao.save(player);
            //不通过日志
            Log log = new Log();
            log.setId(UUID.randomUUID().toString());
            log.setDate(new Date());
            log.setName(player.getName());
            InetAddress ip4 = Inet4Address.getLocalHost();
            System.out.println(ip4.getHostAddress());
            log.setIp(ip4.getHostAddress());
            Dept dept = deptDao.findById(player.getDept().getId()).get();
            log.setContent("单位审核-"+dept.getName()+"-审核失败");
            logDao.save(log);
        }
        return null;
    }

    public void insert(Player player) {
        playerDao.save(player);
    }
    //计算审核中数量
    public String countpass() {
        return playerDao.countpass();
    }
    //计算审核通过数量
    public String countnopass() {
        return playerDao.countnopass();
    }
    //计算审核不通过数量
    public String countunder() {
        return playerDao.countunder();
    }
    //改变状态
    public List<Player> updatestatus(String status) {
        List<Player> byStatus = playerDao.findByStatus(status);

        return byStatus;
    }
    //查看驳回原因
    public void reason(String id, User user) {
        Player player = playerDao.findById(id).get();
        Message message = new Message();
        message.setId(UUID.randomUUID().toString());
        message.setDate(new Date());
        message.setPark(player.getName()+" 审核未通过 ：驳回原因是："+user.getUsername());
        message.setKey(id);
        messageDao.save(message);
    }
    //批量驳回
    public void playerreasonids(Long[] ids, User user) {
        for(Long id:ids){
            Dept dept = deptDao.findById(id+"").get();
            Message message = new Message();
            message.setId(UUID.randomUUID().toString());
            message.setDate(new Date());
            message.setPark(dept.getPerson()+" 审核未通过 ：驳回原因是："+user.getUsername());
            messageDao.save(message);
        }

    }
    //查看驳回原因
    public Message lookturn(String id) {
       return messageDao.findByKey(id);
    }
    //年限确认
    public List<Player> Annual() {

        List<Player> all = playerDao.findAll();
        ArrayList<Player> list1 = new ArrayList<>();
        Player players = null;
       for (Player aa:all){
           Calendar cal = Calendar.getInstance();
           cal.setTime(aa.getYear1());   //设置当前时间
           cal.add(Calendar.YEAR, +2);  //在当前时间基础上加一年
           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
           String format1 =  format.format(cal.getTime());
           players =  playerDao.limityear2(format1);
           list1.add(players);
       }
       //去除重复
        List<Player> temp = new ArrayList<Player>();
        Iterator<Player> iterator = list1.iterator();
        while (iterator.hasNext()) {
            Player str = iterator.next();
            if (!temp.contains(str)) {
                temp.add(str);
            }
        }
        for (Player s : temp) {
            System.out.println(s);
        }
        return temp;
    }
    //再次注册
    public List<Player> beginregister() {
        List<Player> all = playerDao.findAll();
        Player players2 = null;
        ArrayList<Player> list2 = new ArrayList<>();
        for (Player aa:all){
            System.out.println(aa.getYear2());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String format1 =  format.format(aa.getYear2());
            System.out.println(format1);
            players2 =  playerDao.limityear2(format1);
            list2.add(players2);
        }
        //去除重复
        List<Player> temp2 = new ArrayList<Player>();
        Iterator<Player> iterator = list2.iterator();
        while (iterator.hasNext()) {
            Player str = iterator.next();
            if (!temp2.contains(str)) {
                temp2.add(str);
            }
        }
        for (Player s : temp2) {
            System.out.println(s);
        }
        return temp2;
    }
    //首次注册
    public List<Player> register() {

        List<Player> all = playerDao.findAll();
        ArrayList<Player> list2 = new ArrayList<>();
        Player players = null;
        for (Player aa:all){
            Calendar cal = Calendar.getInstance();
            cal.setTime(aa.getYear2());   //设置当前时间
            cal.add(Calendar.YEAR, -3);  //在当前时间基础上加一年
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String format1 =  format.format(cal.getTime());
            players =  playerDao.limityear2(format1);
            list2.add(players);
        }
        //去除重复
        List<Player> temp3 = new ArrayList<Player>();
        Iterator<Player> iterator = list2.iterator();
        while (iterator.hasNext()) {
            Player str = iterator.next();
            if (!temp3.contains(str)) {
                temp3.add(str);
            }
        }
        for (Player s : temp3) {
            System.out.println(s);
        }
        return temp3;
    }
}
